using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class JobSeekers_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Contents["jsuserid"] != null)
        {
            Response.Redirect("myaccount.aspx");
        }
        if (Request.Cookies["Remember"] != null)
        {
            HttpCookie httpCookie =
            Request.Cookies["EmailAndTime"];
            txtEmail.Text = httpCookie.Values["Email"].ToString();

        }
        if (Session["JSLogin"] == null)
            Session["JSLogin"] = 1;
            
        if(this.PreviousPage != null)
        {
            
            Page page = this.PreviousPage;
            TextBox textBoxEmail = (TextBox)page.FindControl("txtEmail");
            TextBox textBoxPassword = (TextBox)page.FindControl("txtPassword");
            txtEmail.Text = textBoxEmail.Text;
            Login(textBoxEmail.Text, textBoxPassword.Text);
             
        }
        
            
         if (Convert.ToInt32(Session["JSLogin"]) >= 3)
        {
            Session["JSLogin"] =
                Convert.ToInt32(Session["JSLogin"]) + 1;
            Common common = new Common();
            string randomWord = common.RandomWord(9000 + Convert.ToInt32(Session["JSLogin"]));
            imageRestrict.ImageUrl = "~/RestrictLogin.ashx?word=" + randomWord;
            CompareValidator1.ValueToCompare = randomWord;            
            Restrict.Visible = true;
        }
        else
            Restrict.Visible = false;
    }
     
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            if (Session["JSLogin"] != null)                 
                Session["JSLogin"] =
                Convert.ToInt32(Session["JSLogin"]) + 1;

            Login(txtEmail.Text, txtPassword.Text);
            
         }
         catch(Exception ex)
         {
            literalError.Text = ex.Message;
         }
    }
    void Login(string email, string password)
    {
        string[] valueToGet = new string[4] { "", "", "", "" };
        RecruitVietnam cls = new RecruitVietnam();
        cls.ConnectionString = WebConfigurationManager.ConnectionStrings[
           "RecruitVietnam"].ConnectionString;
        valueToGet = cls.LoginAuthentication(email, password, "0");
        if (valueToGet[0] == "OK")
        {
            Session.Add("jsuserid", valueToGet[1]);
            Session.Add("jsemail", email);
            Session.Add("jsfullname", valueToGet[2]);
            Session.Add("jsresume", valueToGet[3]);
            Session.Add("jscoverletter", valueToGet[4]);
            Session.Remove("JSLogin");
            WriteCookie(email);
            if (Session["jsCurrentPage"] != null)
                Response.Redirect(Session["jsCurrentPage"].ToString());
            else
                Response.Redirect("myaccount.aspx");

        }
        else
        {
            switch (valueToGet[0])
            {
                case "U":
                    lblHeader.Text = "Email is not existing," +
                    " please re-enter your email and try again.";
                    break;
                case "P":
                    lblHeader.Text = "Invalid password, " +
                    "please re-enter your own password and try again.";
                    break;
                default:
                    lblHeader.Text = "System error, " + cls.ErrorMessage;
                    break;
            }
        }
    }
    void WriteCookie(string email)
    {
        if (CheckBoxRememberEmail.Checked)
        {
            HttpCookie httpCookie = new HttpCookie("Remember", "Yes");
            Response.Cookies.Add(httpCookie);
            httpCookie = new HttpCookie("EmailAndTime");
            httpCookie.Values.Add("Email",email);
            httpCookie.Values.Add("Time", DateTime.Now.ToShortDateString());
            Response.Cookies.Add(httpCookie);
        }
    }
}
